package com.tedu.ds.sessionAndMessage.service.Impl;

import com.tedu.ds.customer.mapper.CustomerMapper;
import com.tedu.ds.customer.pojo.dto.CustomerQuery;
import com.tedu.ds.customer.pojo.vo.CustomerVO;
import com.tedu.ds.employee.mapper.EmployeeMapper;
import com.tedu.ds.employee.pojo.dto.EmployeeQuery;
import com.tedu.ds.employee.pojo.vo.EmployeeVO;
import com.tedu.ds.sessionAndMessage.mapper.SessionAndMessageMapper;
import com.tedu.ds.sessionAndMessage.pojo.dto.MessageParam;
import com.tedu.ds.sessionAndMessage.pojo.dto.MessageQuery;
import com.tedu.ds.sessionAndMessage.pojo.dto.SessionParam;
import com.tedu.ds.sessionAndMessage.pojo.dto.SessionQuery;
import com.tedu.ds.sessionAndMessage.pojo.entity.SMessage;
import com.tedu.ds.sessionAndMessage.pojo.entity.Session;
import com.tedu.ds.sessionAndMessage.pojo.vo.MessageVO;
import com.tedu.ds.sessionAndMessage.pojo.vo.SessionVO;
import com.tedu.ds.sessionAndMessage.service.SessionAndMessageService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SessionAndMessageServiceImpl implements SessionAndMessageService {
    @Autowired
    private SessionAndMessageMapper sessionAndMessageMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public List<SessionVO> sessionSelect(SessionQuery sessionQuery) {
        if (sessionQuery.getCsName()!=null){
            EmployeeQuery employeeQuery=new EmployeeQuery();
            employeeQuery.setUsername(sessionQuery.getCsName());
            List<EmployeeVO> employeeVOList= employeeMapper.select(employeeQuery);
            if (employeeVOList.size()>0){
                List<Long> ids=new ArrayList<>();
                employeeVOList.forEach(employeeVO -> ids.add((employeeVO.getId())));
                sessionQuery.setCsIds(ids);
            }else {
                return null;
            }
        }else if (sessionQuery.getCustName()!=null){
            CustomerQuery customerQuery=new CustomerQuery();
            customerQuery.setUsername(sessionQuery.getCustName());
            List<CustomerVO> customerVOList=customerMapper.select(customerQuery);
            if (customerVOList.size()>0){
                List<Long> ids=new ArrayList<>();
                customerVOList.forEach(customerVO -> ids.add(customerVO.getId()));
                sessionQuery.setCustIds(ids);
            }else {
                return null;
            }
        }
        List<SessionVO> list=sessionAndMessageMapper.selectSession(sessionQuery);
        for (SessionVO s : list){
            if (s.getCsId()!=null){
                EmployeeQuery employeeQuery=new EmployeeQuery();
                employeeQuery.setId(s.getCsId());
                List<EmployeeVO> employeeVOList=employeeMapper.select(employeeQuery);
                if (employeeVOList.size()>0){
                    s.setCsName(employeeVOList.get(0).getUsername());
                }else {
                    s.setCsName("不存在");
                }
            }
            if (s.getCustId()!=null){
                CustomerQuery customerQuery=new CustomerQuery();
                customerQuery.setId(s.getCustId());
                List<CustomerVO> customerVOList=customerMapper.select(customerQuery);
                if (customerVOList.size()>0){
                    s.setCustName(customerVOList.get(0).getUsername());
                }else {
                    s.setCustName("不存在");
                }
            }
        }
        return list;
    }

    @Override
    public void saveSession(SessionParam sessionParam) {
        Session session=new Session();
        BeanUtils.copyProperties(sessionParam,session);
        if (session.getId()==null){
            if (session.getCsId()==null){
                List<Long> csIds = sessionAndMessageMapper.getCsId();
                session.setCsId(csIds.get(0));
            }
            sessionAndMessageMapper.insertSession(session);
            //联查
            //查询客服id，客服对应的正在进行中的会话id数量
            //连接条件：客服id=员工id
            //where 部门是客服 在线 会话状态 = 进行中
            //分组 客服id
            //排序 进行中数量 升序
            //第一个是工作量最少的客服
            //把新增的会话分配给第一个客服
        }else {
            sessionAndMessageMapper.updateSession(session);
        }
    }

    @Override
    public void deleteSession(Integer id) {
        sessionAndMessageMapper.deleteSession(id);
    }

    @Override
    public List<MessageVO> messageSelect(MessageQuery messageQuery) {
        List<MessageVO> list=sessionAndMessageMapper.selectMessage(messageQuery);
        for (MessageVO m : list){
            if (m.getCsId()!=null){
                EmployeeQuery employeeQuery=new EmployeeQuery();
                employeeQuery.setId(m.getCsId());
                List<EmployeeVO> employeeVOList=employeeMapper.select(employeeQuery);
                if (employeeVOList.size()>0){
                    m.setCsName(employeeVOList.get(0).getUsername());
                }else {
                    m.setCsName("不存在");
                }
            }
            if (m.getCustId()!=null){
                CustomerQuery customerQuery=new CustomerQuery();
                customerQuery.setId(m.getCustId());
                List<CustomerVO> customerVOList=customerMapper.select(customerQuery);
                if (customerVOList.size()>0){
                    m.setCustName(customerVOList.get(0).getUsername());
                }else {
                    m.setCustName("不存在");
                }
            }
        }
        return list;
    }

    @Override
    public void saveMessage(MessageParam messageParam) {
        SMessage message=new SMessage();
        BeanUtils.copyProperties(messageParam,message);
        if (message.getId()==null){
            sessionAndMessageMapper.insertMessage(message);
        }else {
            sessionAndMessageMapper.updateMessageStatus(message);
        }
    }

    @Override
    public void deleteMessage(List<Long> ids) {
        sessionAndMessageMapper.deleteMessage(ids);
    }
}
